package com.example.studentmanager.common.configure;


import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


/**
 * 设置数据源监控配置
 */


@Configuration
public class DruidConfigure {

    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druid(){
        return new DruidDataSource();
    }

    @Bean
    public ServletRegistrationBean  statViewResolver(){
        ServletRegistrationBean registrationBean=new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
        Map<String,String>   map=new ConcurrentHashMap<String,String>();
        map.put("loginUserName","root");
        map.put("loginPassWord","12345");
        map.put("deny","192.168.2.122");
        map.put("allow","127.0.0.1");
        registrationBean.setInitParameters(map);
        return registrationBean;
    }


    @Bean
    public FilterRegistrationBean webStatFilter(){
        FilterRegistrationBean filterRegistrationBean=new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WebStatFilter());
        ConcurrentHashMap<String,String> concurrentHashMap=new ConcurrentHashMap<String,String>();
        concurrentHashMap.put("exclusios","*.js,*.css,/druid/*");
        filterRegistrationBean.setInitParameters(concurrentHashMap);
        filterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));
        return filterRegistrationBean;
    }
}
